<?php
/******************************************************************************
 *
 *    OpenZoowa - Web App to manage events
 *    Copyright (C) 2008 Jesus Ruiz-Ayucar
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU Affero General Public License as
 *    published by the Free Software Foundation, either version 3 of the
 *    License, or (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *****************************************************************************/
   if (!defined('ZOOWA')) {
      include_once('../../config.php');
   }
   require_once(modelspath . 'event.php');

   class Comment {
      private $id;
      private $user;
      private $event;
      private $date_post;
      private $text;

      public function Comment($id = null) {
         if (isset($id) && !empty($id)) {
            global $db;

            $SQL = 'SELECT * FROM '.$GLOBALS["db_pre"].'comments ';
            $SQL .= 'WHERE id = ' . $db->escape($id);
            $res = $db->get_row($SQL);
            $this->id = $id;
            $this->user = $res->user_id;
            $this->event = $res->event_id;
            $this->date_post = $res->date_post;
            $this->text = $res->comment;
         } else {
            $id = null;
            $user = null;
            $event = null;
            $date_post = '';
            $comment = '';
         }
      }

      public function add() {
         global $db;

         if (isset($this->event) &&
            isset($this->user) &&
            !empty($this->text))
         {
            $SQL  = 'INSERT INTO '.$GLOBALS['db_pre'].'comments ';
            $SQL .= '(user_id, event_id, date_post, comment) VALUES (';
            $SQL .= $db->escape($this->user->getId()).', ';
            $SQL .= $db->escape($this->event->getId()).', ';
            $SQL .= 'NOW(), ';
            $SQL .= '"'.$db->escape($this->text).'")';

            if ($db->query($SQL)) {
                  $this->id = $db->insert_id;
                  return $this->id;
            } else {
               return 0;
            }
         } else {
            return 0;
         }
      }

      public function update() {
         global $db;

         $SQL  = 'UPDATE '.$GLOBALS['db_pre'].'comments SET ';
         $SQL .= 'comment = "'.$db->escape($this->text).'" ';
         $SQL .= 'WHERE id = '.$db->escape($this->id);

         return $db->query($SQL);
      }

      public function del() {
         global $db;

         if (isset($this->id)) {
            $SQL  = 'DELETE FROM comments WHERE ';
            $SQL .= 'id = '.$db->escape($this->id);
            return $db->query($SQL);
         }
      }

      public function setEvent($event_id) {
         $this->event = new Event($event_id);
      }

      public function setUser($user_id) {
         $this->user = new User($user_id);
      }

      public function setText($text) {
         $this->text = strip_tags(stripslashes($text),'<p><a><ul><ol><li><em><strong><br><img><object><param><embed>');
      }

      public function getId() {
         return $this->id;
      }

      public function getUser() {
         if(!is_a($this->user, 'User')) {
            $this->user = new User($this->user);
         }
         return $this->user;
      }

      public function getEvent() {
         if(!is_a($this->event, 'Event')) {
            $this->event = new Event($this->event);
         }
         return $this->event;
      }

      public function getDatePost() {
         return Util::format_date($this->date_post);
      }

      public function getText() {
         return $this->text;
      }

      public static function getCommentsFromEventId($event_id) {
         global $db;

         $SQL  = 'SELECT id FROM comments WHERE ';
         $SQL .= 'event_id = '.$db->escape($event_id).' ';
         $SQL .= 'ORDER BY date_post desc';

         $comments = $db->get_col($SQL);
         if (isset($comments)) {
            foreach($comments as $comment) {
               $comment_list[] = new Comment($comment);
            }
         }
         return $comment_list;
      }
   }
?>